package com.raptor.mobile.samples;

import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

public class ActivityAdapter extends RecyclerView.Adapter<ActivityAdapter.CustomViewHolder> {
    private final List<ActivityInfo> mData;
    private final Context mContext;

    public ActivityAdapter(Context context, List<ActivityInfo> data) {
        mContext = context;
        mData = data;
    }

    @Override
    public CustomViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.simple_two_item_list, parent, false);
        return new CustomViewHolder(view);
    }

    @Override
    public void onBindViewHolder(CustomViewHolder holder, final int position) {
        holder.bindData(mData.get(position));
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.setClassName(mContext, mData.get(position).name);
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                mContext.startActivity(intent);
            }
        });
    }

    @Override
    public int getItemCount() {
        return mData.size();
    }

    public class CustomViewHolder extends RecyclerView.ViewHolder {
        public CustomViewHolder(View itemView) {
            super(itemView);
        }

        public void bindData(ActivityInfo data) {
            ((TextView) itemView.findViewById(R.id.title)).setText(mContext.getResources().getString(data.labelRes));
            ((TextView) itemView.findViewById(R.id.content)).setText(data.name);
        }
    }
}
